package com.laker.admin.module.comm.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laker.admin.framework.aop.metrics.Metrics;
import com.laker.admin.framework.aop.trace.LakerTrace;
import com.laker.admin.framework.model.PageResponse;
import com.laker.admin.framework.model.Response;
import com.laker.admin.framework.model.ResultTree;
import com.laker.admin.module.comm.entity.CommSpecs;
import com.laker.admin.module.comm.entity.CommSpecsRelation;
import com.laker.admin.module.comm.service.ICommSpecsRelationService;
import com.laker.admin.module.comm.service.ICommSpecsService;
import com.laker.admin.module.sys.entity.SysRole;
import com.laker.admin.module.sys.entity.SysUserRole;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author laker
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/comm/specs")
@Metrics
@LakerTrace
public class CommSpecsController {
    @Autowired
    ICommSpecsService commSpecsService;

    @Autowired
    ICommSpecsRelationService commSpecsRelationService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    public PageResponse pageAll(@RequestParam(required = false, defaultValue = "1") long page,
                                @RequestParam(required = false, defaultValue = "10") long limit,
                                String keyWord) {
        Page roadPage = new Page<>(page, limit);
        LambdaQueryWrapper<CommSpecs> queryWrapper = new QueryWrapper().lambda();

        queryWrapper.eq(CommSpecs::getDelFlag, 0).and(StrUtil.isNotBlank(keyWord), i -> i.
                        like(StrUtil.isNotBlank(keyWord), CommSpecs::getCommSpecsName, keyWord));

        Page pageList = commSpecsService.page(roadPage, queryWrapper);
        return PageResponse.ok(pageList.getRecords(), pageList.getTotal());
    }

    @PostMapping
    @ApiOperation(value = "新增或者更新")
    public Response saveOrUpdate(@RequestBody CommSpecs param) {
        return Response.ok(commSpecsService.saveOrUpdate(param));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Response get(@PathVariable Long id) {
        return Response.ok(commSpecsService.getById(id));
    }

    @GetMapping("/specsList")
    public Response specsList(Long commId) {
        List<CommSpecs> allData = commSpecsService.list(Wrappers.<CommSpecs>lambdaQuery()
                .eq(CommSpecs::getDelFlag, 0)
                .orderByAsc(CommSpecs::getCreatedTime));
        if (commId != null) {

            QueryWrapper<CommSpecsRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ct.comm_id", commId);
            queryWrapper.eq("ct.del_flag", 0L);

            List<CommSpecsRelation> myData = commSpecsRelationService.list(queryWrapper);
            allData.forEach(allD -> {
                myData.forEach(myD -> {
                    if (allD.getCommSpecsId().equals(myD.getCommSpecsId())) {
                        allD.setChecked(true);
                    }
                });
            });
        }

        return Response.ok(allData);
    }
}
